package com.hatricks.is.redission.distributedlock;

import com.hatricks.is.redission.ILock;
import com.hatricks.is.redission.ILockKeyEnum;
import org.jetbrains.annotations.NotNull;
import org.redisson.api.RFuture;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;

/**
 * @author: yanyh
 * @date: 2023年5月29日14:58:07
 */
public class DistributedLock implements ILock {
    RLock rLock;
    ILockKeyEnum lockKeyEnum;

    public DistributedLock(RLock rLock) {
        this.rLock = rLock;
    }

    public DistributedLock(RLock rLock, ILockKeyEnum lockKeyEnum) {
        this.rLock = rLock;
        this.lockKeyEnum = lockKeyEnum;
    }

    @Override
    public String getName() {
        return rLock.getName();
    }

    @Override
    public void lockInterruptibly(long l, TimeUnit timeUnit) throws InterruptedException {
        rLock.lockInterruptibly(l, timeUnit);
    }

    @Override
    public boolean tryLock(long l, long l1, TimeUnit timeUnit) throws InterruptedException {
        return rLock.tryLock(l, l1, timeUnit);
    }

    @Override
    public void lock(long l, TimeUnit timeUnit) {
        rLock.lock(l, timeUnit);
    }

    @Override
    public boolean forceUnlock() {
        return rLock.forceUnlock();
    }

    @Override
    public boolean isLocked() {
        return rLock.isLocked();
    }

    @Override
    public boolean isHeldByThread(long l) {
        return rLock.isHeldByThread(l);
    }

    @Override
    public boolean isHeldByCurrentThread() {
        return rLock.isHeldByCurrentThread();
    }

    @Override
    public int getHoldCount() {
        return rLock.getHoldCount();
    }

    @Override
    public long remainTimeToLive() {
        return rLock.remainTimeToLive();
    }

    @Override
    public void lock() {
        if (lockKeyEnum == null) {
            rLock.lock();
        } else {
            rLock.lock(lockKeyEnum.getSeconds(), TimeUnit.SECONDS);
        }
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        rLock.lockInterruptibly();
    }

    @Override
    public boolean tryLock() {
        return rLock.tryLock();
    }

    @Override
    public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
        return rLock.tryLock(time, unit);
    }

    @Override
    public void unlock() {
        rLock.unlock();
    }

    @Override
    public Condition newCondition() {
        return rLock.newCondition();
    }

    @Override
    public RFuture<Boolean> forceUnlockAsync() {
        return rLock.forceUnlockAsync();
    }

    @Override
    public RFuture<Void> unlockAsync() {
        return rLock.unlockAsync();
    }

    @Override
    public RFuture<Void> unlockAsync(long l) {
        return rLock.unlockAsync(l);
    }

    @Override
    public RFuture<Boolean> tryLockAsync() {
        return rLock.tryLockAsync();
    }

    @Override
    public RFuture<Void> lockAsync() {
        return rLock.lockAsync();
    }

    @Override
    public RFuture<Void> lockAsync(long l) {
        return rLock.lockAsync(l);
    }

    @Override
    public RFuture<Void> lockAsync(long l, TimeUnit timeUnit) {
        return rLock.lockAsync(l, timeUnit);
    }

    @Override
    public RFuture<Void> lockAsync(long l, TimeUnit timeUnit, long l1) {
        return rLock.lockAsync(l, timeUnit, l1);
    }

    @Override
    public RFuture<Boolean> tryLockAsync(long l) {
        return rLock.tryLockAsync(l);
    }

    @Override
    public RFuture<Boolean> tryLockAsync(long l, TimeUnit timeUnit) {
        return rLock.tryLockAsync(l, timeUnit);
    }

    @Override
    public RFuture<Boolean> tryLockAsync(long l, long l1, TimeUnit timeUnit) {
        return rLock.tryLockAsync(l, l1, timeUnit);
    }

    @Override
    public RFuture<Boolean> tryLockAsync(long l, long l1, TimeUnit timeUnit, long l2) {
        return rLock.tryLockAsync(l, l1, timeUnit, l2);
    }

    @Override
    public RFuture<Integer> getHoldCountAsync() {
        return rLock.getHoldCountAsync();
    }

    @Override
    public RFuture<Boolean> isLockedAsync() {
        return rLock.isLockedAsync();
    }

    @Override
    public RFuture<Long> remainTimeToLiveAsync() {
        return rLock.remainTimeToLiveAsync();
    }
}
